/*会服管理插件*/

// 会议准备服务URL
var prepareServiceUrl;

// 会议清理服务URL
var cleanServiceUrl;

// 会议服务时间选择Id
var confServiceDateId;

// 会议准备服务列表ID
var prepareServiceTableId;

// 会议清理服务列表ID
var cleanServiceTableId;

// 会议准备服务下拉框ID
var preareServiceSelectId;

// 会议清理服务下拉框ID
var cleanServiceSelectId;

// 是否选中服务准备当前天
var isPrepareSelect = true;

// 是否选中服务清理当前天
var isCleanSelect = true;

//准备会议对话框
var detailServiceDialog;

//清理会议对话框
var detailCleanDialog;

// 服务人员URL
var servicePersonnelURL;

// 服务人员id
var servicePersonnelData;

// 准备人员保存URL
var savePrepareUrl;

// 清理人员保存URL
var saveCleanUrl;

/**
 * 设置会议准备服务URL
 */
function setPerareServiceUrl(url) {
    prepareServiceUrl = url;
}

/**
 * 设置会议准备保存URL
 */
function setSavePrepareUrl(url) {
    savePrepareUrl = url;
}

/**
 * 设置会议清理保存URL
 */
function setSaveCleanUrl(url) {
    saveCleanUrl = url;
}


/**
 * 查找服务人员
 */
function setServicePersonnelUrl(url) {
    servicePersonnelURL = url;
}

/**
 * 设置会议清理服务URL
 */
function setCleanServiceUrl(url) {
    cleanServiceUrl = url;
}

function setPrepareSelect(boor) {
    if (boor == "1") {
        isPrepareSelect = true;
    } else {
        isPrepareSelect = false;
    }
}

function setCleanSelect(boor) {
    if (boor == "1") {
        isCleanSelect = true;
    } else {
        isCleanSelect = false;
    }
}

/**
 * 设置会议准备服务下拉框ID
 */
function setPerareServiceSelectId(id) {
    preareServiceSelectId = id;

    $(preareServiceSelectId).change(function () {
        var preDateType = $("#prepareTable").find("#prepareServiceInterval").val();
        $("#detailServiceDialog").find("#predateType").val(preDateType);
        $("#detailCleanDialog").find("#predateType").val(preDateType);
        if ($(this).val() == "1") {
            if (!isPrepareSelect) {
                isPrepareSelect = true;
                var currentTime = new Date();
                time1 = formatDate(currentTime) + " 00:00:00";
                time2 = formatDate(currentTime) + " 23:59:59";
                data = getConfServiceData(prepareServiceUrl, time1, time2);
                createPrepareServiceTable(data);
            }
        } else {
            if (isPrepareSelect) {
                isPrepareSelect = false;
                var currentTime = new Date();
                time1 = formatDate(currentTime) + " 00:00:00";
                currentTime.setDate(currentTime.getDate() + 6);
                time2 = formatDate(currentTime) + " 23:59:59";
                data = getConfServiceData(prepareServiceUrl, time1, time2);
                createPrepareServiceTable(data);
            }
        }
    });
}

/**
 * 设置会议清理服务下拉框ID
 */
function setCleanServiceSelectId(id) {
    cleanServiceSelectId = id;
    $(cleanServiceSelectId).change(function () {
        var cleDateType = $("#cleanTable").find("#cleanServiceInterval").val();
        $("#detailCleanDialog").find("#cledateType").val(cleDateType);
        $("#detailServiceDialog").find("#cledateType").val(cleDateType);
        if ($(this).val() == "1") {
            if (!isCleanSelect) {
                isCleanSelect = true;
                var currentTime = new Date();
                time1 = formatDate(currentTime) + " 00:00:00";
                time2 = formatDate(currentTime) + " 23:59:59";
                data = getConfServiceData(cleanServiceUrl, time1, time2);
                createCleanServiceTable(data);
            }
        } else {
            if (isCleanSelect) {
                isCleanSelect = false;
                var currentTime = new Date();
                time1 = formatDate(currentTime) + " 00:00:00";
                currentTime.setDate(currentTime.getDate() + 6);
                time2 = formatDate(currentTime) + " 23:59:59";
                data = getConfServiceData(cleanServiceUrl, time1, time2);
                createCleanServiceTable(data);
            }
        }
    });
}

/**
 * 设置会议准备列表
 */
function setPerareServiceTable(id) {
    prepareServiceTableId = id;

    // 会议列表行选中事件
    $(prepareServiceTableId).bind("rowselected", function (event, data, row, col) {
        if ($().find(".option_remove") != null) {
            $(detailServiceDialog).find(".option_remove").remove();
        }
        confData = data;
        selectedConfTableRow = row;
        selectedConfTableCol = col;
        if (col != 7) {
            $(detailServiceDialog).initCustomDialog({
                title: "会议准备",
                width: 600,
                height: 580,
                button1: "保存",
                disabled1: false,
                button2: "取消",
                disabled2: false,
                buttonSize: 2,
                modal: true
            });
            $(detailServiceDialog).openCustomDialog();
            $(detailServiceDialog).find("#meetingId").val(confData.meetingId);
            $(detailServiceDialog).find("#papareTime").val(confData.papareTime);
            $(detailServiceDialog).find("#meetingName").val(confData.meetingName);
            $(detailServiceDialog).find("#boardroom").val(confData.boardroom);
            $(detailServiceDialog).find("#device").val(confData.device);
            $(detailServiceDialog).find("#service").val(confData.service);
            var oriName = confData.preStaffer;
            data = getServicePersonnelData(servicePersonnelURL);
            $(detailServiceDialog).find("#preStaffer").append("<option class=\"option_remove\" value=\"\">--</option>");
            for (var i = 0; i < data.length; i++) {
                if (oriName != data[i].stafferName) {
                    $(detailServiceDialog).find("#preStaffer").append("<option class=\"option_remove\" value=" + data[i].stafferName + ">" + data[i].stafferName + "</option>");
                } else {
                    $(detailServiceDialog).find("#preStaffer").append("<option class=\"option_remove\" selected=\"selected\" value=" + data[i].stafferName + ">" + data[i].stafferName + "</option>");
                }
            }
        }
    });
}

/**
 * 设置会议清理列表
 */
function setCleanServiceTable(id) {
    cleanServiceTableId = id;

    // 会议列表行选中事件
    $(cleanServiceTableId).bind("rowselected", function (event, data, row) {
        if ($().find(".option_remove") != null) {
            $(detailCleanDialog).find(".option_remove").remove();
        }
        confData = data;
        selectedConfTableRow = row;
        $(detailCleanDialog).initCustomDialog({
            title: "会议清理",
            width: 600,
            height: 380,
            button1: "保存",
            disabled1: false,
            button2: "取消",
            disabled2: false,
            buttonSize: 2,
            modal: true
        });
        $(detailCleanDialog).openCustomDialog();
        $(detailCleanDialog).find("#meetingId").val(confData.meetingId);
        $(detailCleanDialog).find("#boardroom").val(confData.boardroom);
        $(detailCleanDialog).find("#cleanTime").val(confData.cleanTime);
        var oriName = confData.cleStaffer;
        data = getServicePersonnelData(servicePersonnelURL);
        $(detailCleanDialog).find("#preStaffer").append("<option class=\"option_remove\" value=\"\">--</option>");
        for (var i = 0; i < data.length; i++) {
            if (oriName != data[i].stafferName) {
                $(detailCleanDialog).find("#preStaffer").append("<option class=\"option_remove\" value=" + data[i].stafferName + ">" + data[i].stafferName + "</option>");
            } else {
                $(detailCleanDialog).find("#preStaffer").append("<option class=\"option_remove\" selected=\"selected\" value=" + data[i].stafferName + ">" + data[i].stafferName + "</option>");
            }

        }
    });
}

/**
 * 实时获取会议数据
 */
function getConfServiceData(url, time1, time2) {
    try {
        var obj = $.ajax({
            url: url,
            data: {startTime: time1, endTime: time2},
            async: false,
            timeout: 60000
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 服务人员列表
 */
function getServicePersonnelData(url) {
    try {
        var obj = $.ajax({
            url: url,
            data: {},
            async: false,
            timeout: 60000
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 创建会议列表
 */
function createPrepareServiceTable(data) {
    try {
        if ($().find(".middle_2_bottom_table_1") != null) {
            $(prepareServiceTableId).find(".middle_2_bottom_table_1").remove();
        }
        for (var i = 0; i < data.length; i++) {
            var j = i + 1;
            $(prepareServiceTableId).append("<tr class='middle_2_bottom_table_1' ></tr>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + j + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].papareTime + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].meetingName + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].boardroom + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].device + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].service + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].preStaffer + "</td>");
            $(prepareServiceTableId).find("tr").eq(i + 1).append("<td align='center'><a title='查看图片' href='#'><img src='/foc/resources/images/admin/check_xiao.png' border='0'   onclick=\"showImage('" + data[i].image + "')\" /></a></td>");

        }
        $(prepareServiceTableId).initDataTable({
            rowSelected: true,
            colSelected: true,
            data: data
        });
    } catch (e) {
        $(prepareServiceTableId).initDataTable({
            rowSelected: true,
            data: data
        });
    }
}

function createCleanServiceTable(data) {
    try {
        if ($().find(".middle_2_bottom_table_1") != null) {
            $(cleanServiceTableId).find(".middle_2_bottom_table_1").remove();
        }
        for (var i = 0; i < data.length; i++) {
            var j = i + 1;
            $(cleanServiceTableId).append("<tr class='middle_2_bottom_table_1' ></tr>");
            $(cleanServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + j + "</td>");
            $(cleanServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].cleanTime + "</td>");
            $(cleanServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].boardroom + "</td>");
            $(cleanServiceTableId).find("tr").eq(i + 1).append("<td align='center'>" + data[i].cleStaffer + "</td>");
        }
        $(cleanServiceTableId).initDataTable({
            rowSelected: true,
            data: data
        });
    } catch (e) {
        $(cleanServiceTableId).initDataTable({
            rowSelected: true,
            data: data
        });
    }
}

/**
 * 初始化会议审批JS
 */
function initConfService(interval) {
    if (interval == null || interval.length == 0) {
        interval = 5000;
    }
    if (isPrepareSelect) {
        var currentTime = new Date();
        time1 = formatDate(currentTime) + " 00:00:00";
        time2 = formatDate(currentTime) + " 23:59:59";
        data = getConfServiceData(prepareServiceUrl, time1, time2);
        createPrepareServiceTable(data);
    } else {
        var currentTime = new Date();
        time1 = formatDate(currentTime) + " 00:00:00";
        currentTime.setDate(currentTime.getDate() + 6);
        time2 = formatDate(currentTime) + " 23:59:59";
        data = getConfServiceData(prepareServiceUrl, time1, time2);
        createPrepareServiceTable(data);
    }
    if (isCleanSelect) {
        var currentTime = new Date();
        time1 = formatDate(currentTime) + " 00:00:00";
        time2 = formatDate(currentTime) + " 23:59:59";
        data = getConfServiceData(cleanServiceUrl, time1, time2);
        createCleanServiceTable(data);
    } else {
        var currentTime = new Date();
        time1 = formatDate(currentTime) + " 00:00:00";
        currentTime.setDate(currentTime.getDate() + 6);
        time2 = formatDate(currentTime) + " 23:59:59";
        data = getConfServiceData(cleanServiceUrl, time1, time2);
        createCleanServiceTable(data);
    }
    setInterval(function () {
        if (isPrepareSelect) {
            var currentTime = new Date();
            time1 = formatDate(currentTime) + " 00:00:00";
            time2 = formatDate(currentTime) + " 23:59:59";
            data = getConfServiceData(prepareServiceUrl, time1, time2);
            createPrepareServiceTable(data);
        } else {
            var currentTime = new Date();
            time1 = formatDate(currentTime) + " 00:00:00";
            currentTime.setDate(currentTime.getDate() + 6);
            time2 = formatDate(currentTime) + " 23:59:59";
            data = getConfServiceData(prepareServiceUrl, time1, time2);
            createPrepareServiceTable(data);
        }
        if (isCleanSelect) {
            var currentTime = new Date();
            time1 = formatDate(currentTime) + " 00:00:00";
            time2 = formatDate(currentTime) + " 23:59:59";
            data = getConfServiceData(cleanServiceUrl, time1, time2);
            createCleanServiceTable(data);
        } else {
            var currentTime = new Date();
            time1 = formatDate(currentTime) + " 00:00:00";
            currentTime.setDate(currentTime.getDate() + 6);
            time2 = formatDate(currentTime) + " 23:59:59";
            data = getConfServiceData(cleanServiceUrl, time1, time2);
            createCleanServiceTable(data);
        }
    }, interval);
}

/**
 * createPrepareServiceTable
 * 设置会议审批对话框
 */
function setDetailConfDialog(id) {
    detailServiceDialog = id;

    $(detailServiceDialog).initCustomDialog({
        title: "相关会议",
        width: 600,
        height: 580,
        modal: true
    });
    $(detailServiceDialog).bind("btnClick", function (event, name, value) {
        _this = $(this);
        switch (name) {
            case "保存":
                _this.dialog("close");
                var form = document.getElementById("preForm");
                form.action = savePrepareUrl;
                form.method = "post";
                form.submit();
                break;
            case "取消":
                _this.dialog("close");
                break;
            default:
                break;
        }
    });
}

/**
 * createPrepareServiceTable
 * 设置会议审批对话框
 */
function setDetailCleanDialog(id) {
    detailCleanDialog = id;

    $(detailCleanDialog).initCustomDialog({
        title: "相关会议",
        width: 600,
        height: 580,
        modal: true
    });
    $(detailCleanDialog).bind("btnClick", function (event, name, value) {
        _this = $(this);
        switch (name) {
            case "保存":
                _this.dialog("close");
                var form = document.getElementById("cleForm");
                form.action = saveCleanUrl;
                form.method = "post";
                form.submit();
                break;
            case "取消":
                _this.dialog("close");
                break;
            default:
                break;
        }
    });
}


/**
 * 获取列表选中行
 */
function getSelectedRow(id, confId) {
    var count = $(id).find(".middle_2_bottom_table_1").length;
    for (var i = 0; i < count; i++) {
        if (confId == $(id).find(".middle_2_bottom_table_1").eq(i).find("td").eq(0).find("input").val()) {
            row = i + 1;
            return row;
        }
    }
}

/**
 * @param id
 * @param url
 */
function showImage(url) {
    $("#showImageDialog").openCustomFrmDialog();
    if ($("#showImageDialog").find("#img").attr("src") != "" || $("#showImageDialog").find("#img").attr("src") != null) {
        $("#showImageDialog").find("#img").attr("src", "");
    }
    $("#showImageDialog").find("#img").attr("data-original", url);
    $("img.lazy").lazyload();
}


/**
 * 格式化日期
 */
function formatDate(dt) {
    var curYear = dt.getFullYear();
    var curMonth = (dt.getMonth() + 1);
    if (curMonth < 10) {
        curMonth = "0" + curMonth;
    }
    var curDay = dt.getDate();
    if (curDay < 10) {
        curDay = "0" + curDay;
    }
    return curYear + "-" + curMonth + "-" + curDay;
}